package com.ruoyi.siyun.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.siyun.mapper.SyCourseCategoryMapper;
import com.ruoyi.common.core.domain.entity.SyCourseCategory;
import com.ruoyi.siyun.service.ISyCourseCategoryService;

/**
 * 课程分类Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-23
 */
@Service
public class SyCourseCategoryServiceImpl implements ISyCourseCategoryService 
{
    @Autowired
    private SyCourseCategoryMapper syCourseCategoryMapper;

    /**
     * 查询课程分类
     * 
     * @param categoryId 课程分类主键
     * @return 课程分类
     */
    @Override
    public SyCourseCategory selectSyCourseCategoryByCategoryId(Long categoryId)
    {
        return syCourseCategoryMapper.selectSyCourseCategoryByCategoryId(categoryId);
    }

    /**
     * 查询课程分类列表
     * 
     * @param syCourseCategory 课程分类
     * @return 课程分类
     */
    @Override
    public List<SyCourseCategory> selectSyCourseCategoryList(SyCourseCategory syCourseCategory)
    {
        return syCourseCategoryMapper.selectSyCourseCategoryList(syCourseCategory);
    }

    @Override
    public Integer selectSyCourseCollectNum(SyCourseCategory syCourseCategory) {
        Integer collectNum = syCourseCategoryMapper.selectSyCourseCollectNum(syCourseCategory);
        return collectNum;
    }

    /**
     * 新增课程分类
     * 
     * @param syCourseCategory 课程分类
     * @return 结果
     */
    @Override
    public int insertSyCourseCategory(SyCourseCategory syCourseCategory)
    {
        syCourseCategory.setCreateTime(new Date());
        return syCourseCategoryMapper.insertSyCourseCategory(syCourseCategory);
    }

    /**
     * 修改课程分类
     * 
     * @param syCourseCategory 课程分类
     * @return 结果
     */
    @Override
    public int updateSyCourseCategory(SyCourseCategory syCourseCategory)
    {
        syCourseCategory.setUpdateTime(new Date());
        return syCourseCategoryMapper.updateSyCourseCategory(syCourseCategory);
    }

    /**
     * 批量删除课程分类
     * 
     * @param categoryIds 需要删除的课程分类主键
     * @return 结果
     */
    @Override
    public int deleteSyCourseCategoryByCategoryIds(Long[] categoryIds)
    {
        return syCourseCategoryMapper.deleteSyCourseCategoryByCategoryIds(categoryIds);
    }

    /**
     * 删除课程分类信息
     * 
     * @param categoryId 课程分类主键
     * @return 结果
     */
    @Override
    public int deleteSyCourseCategoryByCategoryId(Long categoryId)
    {
        return syCourseCategoryMapper.deleteSyCourseCategoryByCategoryId(categoryId);
    }

    /**
     * 构建前端树形数据信息
     * @param symbolCategory
     * @return
     */
    @Override
    public List<TreeSelect> selectCourseCategoryTreeList(SyCourseCategory symbolCategory) {
        List<SyCourseCategory> list = syCourseCategoryMapper.selectSyCourseCategoryList(symbolCategory);
        return buildCourseCategoryTreeSelect(list);
    }


    /**
     * 构建前端所需要下拉树结构
     *
     * @param SyCourseCategorys 部门列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildCourseCategoryTreeSelect(List<SyCourseCategory> SyCourseCategorys) {
        List<SyCourseCategory> deptTrees = buildCourseCategoryTree(SyCourseCategorys);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param depts 部门列表
     * @return 树结构列表
     */
    @Override
    public List<SyCourseCategory> buildCourseCategoryTree(List<SyCourseCategory> depts){
        List<SyCourseCategory> returnList = new ArrayList<SyCourseCategory>();
        List<Long> tempList = depts.stream().map(SyCourseCategory::getCategoryId).collect(Collectors.toList());
        for (SyCourseCategory dept : depts)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId()))
            {
                recursionFn(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = depts;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SyCourseCategory> list, SyCourseCategory t){
        // 得到子节点列表
        List<SyCourseCategory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SyCourseCategory tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SyCourseCategory> list, SyCourseCategory t)
    {
        return getChildList(list, t).size() > 0;
    }

    /**
     * 得到子节点列表
     */
    private List<SyCourseCategory> getChildList(List<SyCourseCategory> list, SyCourseCategory t)
    {
        List<SyCourseCategory> tlist = new ArrayList<SyCourseCategory>();
        Iterator<SyCourseCategory> it = list.iterator();
        while (it.hasNext())
        {
            SyCourseCategory n = (SyCourseCategory) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getCategoryId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
}
